<template>
  <div class="book-detail-container">
    <div class="page-header">
      <h2>图书详情</h2>
      <div class="header-actions">
        <el-button @click="goBack">返回</el-button>
        <el-button 
          v-if="userRole === 'admin'" 
          type="primary" 
          @click="editBook">
          编辑
        </el-button>
      </div>
    </div>

    <div v-loading="loading">
      <el-skeleton :rows="6" animated v-if="loading" />

      <div v-else class="book-detail-content">
        <el-descriptions border :column="1" class="book-descriptions">
          <el-descriptions-item label="ID">{{ bookInfo.id }}</el-descriptions-item>
          <el-descriptions-item label="书名">{{ bookInfo.title }}</el-descriptions-item>
          <el-descriptions-item label="作者">{{ bookInfo.author }}</el-descriptions-item>
          <el-descriptions-item label="分类">{{ bookInfo.category }}</el-descriptions-item>
          <el-descriptions-item label="ISBN">{{ bookInfo.isbn }}</el-descriptions-item>
          <el-descriptions-item label="出版社">{{ bookInfo.publisher }}</el-descriptions-item>
          <el-descriptions-item label="出版日期">{{ bookInfo.publishDate }}</el-descriptions-item>
          <el-descriptions-item label="页数">{{ bookInfo.pages }}</el-descriptions-item>
          <el-descriptions-item label="价格">{{ bookInfo.price }}元</el-descriptions-item>
          <el-descriptions-item label="库存">{{ bookInfo.stock }}</el-descriptions-item>
          <el-descriptions-item label="添加时间">{{ bookInfo.createTime }}</el-descriptions-item>
          <el-descriptions-item label="更新时间">{{ bookInfo.updateTime }}</el-descriptions-item>
          <el-descriptions-item label="图书介绍">
            <div class="book-description">{{ bookInfo.description || '暂无介绍' }}</div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
import { getBookDetail } from '@/api/book'
import { ElMessage } from 'element-plus'

// 路由对象和路由实例
const route = useRoute()
const router = useRouter()
// 用户存储
const userStore = useUserStore()
// 获取用户角色
const userRole = computed(() => userStore.role)

// 加载状态
const loading = ref(false)
// 图书信息
const bookInfo = ref({})
// 图书ID
const bookId = computed(() => route.params.id)

// 页面加载时获取图书详情
onMounted(() => {
  if (bookId.value) {
    getBookDetailData()
  } else {
    ElMessage.error('图书ID不能为空')
    goBack()
  }
})

// 获取图书详情
const getBookDetailData = async () => {
  loading.value = true
  try {
    const res = await getBookDetail(bookId.value)
    if (res.code === 200) {
      bookInfo.value = res.data
    } else {
      ElMessage.error(res.msg || '获取图书详情失败')
      goBack()
    }
  } catch (error) {
    console.error('获取图书详情异常:', error)
    ElMessage.error('获取图书详情失败')
    goBack()
  } finally {
    loading.value = false
  }
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 编辑图书
const editBook = () => {
  router.push(`/book/edit/${bookId.value}`)
}
</script>

<style scoped>
.book-detail-container {
  animation: fadeIn 0.3s ease-out;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  background-color: var(--card-background);
  padding: 20px 24px;
  border-radius: var(--radius-medium);
  box-shadow: var(--shadow-base);
}

.page-header h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  color: var(--text-primary);
  display: flex;
  align-items: center;
}

.page-header h2::before {
  content: '';
  display: inline-block;
  width: 4px;
  height: 20px;
  background: linear-gradient(180deg, var(--primary-color), #4d7aff);
  margin-right: 12px;
  border-radius: 2px;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.book-detail-content {
  background-color: var(--card-background);
  padding: 24px;
  border-radius: var(--radius-medium);
  box-shadow: var(--shadow-base);
  transition: box-shadow 0.3s;
}

.book-detail-content:hover {
  box-shadow: var(--shadow-medium);
}

.book-descriptions {
  width: 100%;
}

:deep(.el-descriptions__title) {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 16px;
}

:deep(.el-descriptions__label) {
  width: 120px;
  color: var(--text-primary);
  font-weight: 500;
  background-color: var(--primary-light);
}

:deep(.el-descriptions__content) {
  color: var(--text-regular);
  padding: 16px 24px;
}

:deep(.el-descriptions__body) {
  background-color: var(--card-background);
  border-radius: var(--radius-small);
  overflow: hidden;
}

.book-description {
  white-space: pre-line;
  line-height: 1.6;
  padding: 8px 0;
  color: var(--text-regular);
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .page-header h2 {
    margin-bottom: 12px;
  }
  
  :deep(.el-descriptions__label) {
    width: 100px;
  }
  
  :deep(.el-descriptions__content) {
    padding: 12px 16px;
  }
}
</style> 